//
//  MCPath.m
//  iMagazine2
//
//  Created by dreamRen on 12-11-16.
//  Copyright (c) 2012年 iHope. All rights reserved.
//

#import "MCPath.h"



#define kFM [NSFileManager defaultManager]
#define kUserCacheDir @"kelai"
#define kNSUserDefaults [NSUserDefaults standardUserDefaults]

@implementation MCPath

/**
 @brief 获取当前用户的缓存用户文件夹
 
 @param userId 当前用户的id
 
 @return 
 */
+ (NSString *) getDocumentUserCachePath:(NSString *)userId
{
    if (!userId) {
        return nil;
    }
    return [[self getDocumentStjPath] stringByAppendingPathComponent:userId];
}

/**
 @brief 创建stj缓存文件夹
 
 @return
 */
+ (BOOL) createDocumentStj
{
    NSError *error;

    NSString *stjPath = [self getDocumentStjPath];
    BOOL isDir = NO;
    BOOL existed = [kFM fileExistsAtPath:stjPath isDirectory:&isDir];
    if (existed) {
        if ([[NSFileManager defaultManager] subpathsAtPath:stjPath].count == 0) {
            // createDirectoryInDocWithUserid: 参数改成与用户id相关的唯一的字符串
            [self createDirectoryInDocWithUserid:@"user"];
        }
        return YES;
    }
    
    [kFM createDirectoryAtPath:stjPath withIntermediateDirectories:YES attributes:nil error:&error];
    if ([[NSFileManager defaultManager] subpathsAtPath:stjPath].count == 0) {
        // createDirectoryInDocWithUserid: 参数改成与用户id相关的唯一的字符串
//        [self createDirectoryInDocWithUserid:[kNSUserDefaults objectForKey:@"user"]];
        [self createDirectoryInDocWithUserid:@"user"];
    }

    return YES;

}

/**
 @brief  用user创建用户在stj目录下创建user缓存文件夹
 
 @param userId 当前用户的id
 
 @return 结果
 */
+ (BOOL)createDirectoryInDocWithUserid:(NSString *)userId
{
    if (!userId) {
        return NO;
    }
    NSError *error;
    NSString *path = [NSString stringWithFormat:@"%@/%@",[MCPath getDocumentStjPath],userId];
    BOOL isDir = NO;
    BOOL existed = [kFM fileExistsAtPath:path isDirectory:&isDir];
    if (existed) {
        return YES;
    }
    //用户主目录
    [kFM createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];

    return YES;
}

/**
 @brief 在沙盒Doc路径删除文件夹
 
 @param dirPath 要删除的文件的名字
 
 @return 结果
 */
+ (BOOL)deleteDirectoryInDoc:(NSString *)dirPath
{
    NSError *error;
    BOOL isDir = NO;
    NSString *path = [NSString stringWithFormat:@"%@/%@",[MCPath getDocumentStjPath],dirPath];
    if ([kFM fileExistsAtPath:path isDirectory:&isDir]) {
        if([kFM removeItemAtPath:path error:&error]){
            return YES;
        }else{
            return NO;
        }
    }else{
        return NO;
    }
}


/**
 @brief 清除当前用户的数据
 
 @param result 结果回调
 */
+ (void) cleanUserData:(void(^)(BOOL success, float folderSize)) result
{
    NSString *path = [NSString stringWithFormat:@"%@/%@",[MCPath getDocumentStjPath],@"user"];
    if ([kFM fileExistsAtPath:path]) {
        NSError *error;
        float folderSize = [self folderSizeAtPath:path];
       BOOL cleanResult = [kFM removeItemAtPath:path error:&error];
        result(cleanResult,folderSize);
    }else{
        result(YES,0);
    }
}

/**
 @brief 清除所有用户的数据
 
 @param result 结果回调
 */
+ (void) clearAllData:(void(^)(BOOL success,float folderSize)) result
{
    NSString *path = [self getDocumentStjPath];
    NSError *error;
    if ([kFM fileExistsAtPath:path]) {
        float folderSize = [self folderSizeAtPath:path];
        BOOL cleanResult = [kFM removeItemAtPath:path error:&error];
        result(cleanResult, folderSize);
    }else{
        result(YES, 0);
    }
}


#pragma mark - Private

/**
 @brief 获取缓存文件夹
 
 @return
 */
+ (NSString *) getDocumentStjPath
{
    return [[self getDocumentPath] stringByAppendingPathComponent:kUserCacheDir];
}

//返回沙盒Document路径
+(NSString *)getDocumentPath
{
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    return docPath;
}

//返回沙盒Cache路径
+(NSString *)getCachesPath
{
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    return cachePath;
}

//返回App所在的沙盒路径
+(NSString *)getAppPath
{
    return [NSString stringWithFormat:@"%@/",[[NSBundle mainBundle] resourcePath]];
}

/**
 @brief 计算文件的大小
 
 @param filePath
 
 @return 文件大小 b
 */
+ (long long) fileSizeAtPath:(NSString*) filePath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}

/**
 @brief 计算文件夹大小
 
 @param folderPath
 
 @return 文件夹大小 M
 */
+ (float ) folderSizeAtPath:(NSString*) folderPath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize/(1024.0*1024.0);
}


@end
